package com.yuan.util.object;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对象方法封装类，将Class的所有public方法按照方法名归类 <br>
 * date：2017/12/14 13:50
 * @author YouYuan
 * @version 1.0
 */
public class ObjectMethod {
    Map<String, List<Method>> methodMap = new HashMap<String, List<Method>>();
    List<Method> allMethod = new ArrayList<Method>();
    Class c = null;

    public ObjectMethod(Class c) {
        this.c = c;
        init();
    }

    public List<Method> getMethods(String name) {
        return methodMap.get(name);
    }

    public List<Method> allMethods() {
        return allMethod;
    }

    public Map<String, List<Method>> getMethodMap() {
        return this.methodMap;
    }

    private void init() {
        Method[] ms = c.getMethods();
        for (Method m : ms) {
            //仅仅获取public
            if (Modifier.isPublic(m.getModifiers())) {
                if (m.getDeclaringClass() == Object.class) {
                    continue;
                }
                String name = m.getName();
                List<Method> list = methodMap.get(name);
                if (list == null) {
                    list = new ArrayList<Method>(1);
                    methodMap.put(name, list);
                }
                list.add(m);
                allMethod.add(m);
            }

        }

    }
}
